package cc.rboot.topic.core;

import cc.rboot._core.holder.AppContextHolder;
import cc.rboot.topic.annotation.MessageConsumer;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

public abstract class AbstractMessageConsumer<T> implements IMessageConsumer<T> {

    private IMessageTopic<T> topic;

    @Resource
    AppContextHolder appContextHolder;

    @PostConstruct
    public void construct() {
        MessageConsumer messageConsumer = this.getClass().getAnnotation(MessageConsumer.class);
        Class<? extends IMessageTopic> topicClass = messageConsumer.topic();
        this.topic = appContextHolder.getBean(topicClass);
        this.topic.registerConsumers(this);
    }

    @Override
    public IMessageTopic<T> topic() {
        return this.topic;
    }

    @Override
    public String name() {
        MessageConsumer messageConsumer = this.getClass().getAnnotation(MessageConsumer.class);
        return messageConsumer.name();
    }
}
